package models.venue.curriculum;

import enums.GoodsType;
import jodd.bean.BeanCopy;
import models.AdminSupplier;
import models.BaseModel;
import models.constants.DeletedStatus;
import models.merchant.Merchant;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 课程
 * Created by hanbin on 2017/4/5.
 */
@Entity
@Table(name="curriculum_infos")
public class Curriculum extends BaseModel {


    /**
     * 课程名称
     */
    @Column(name="curriculum_name",length = 255)
    public String curriculumName;


    /**
     * 供应商
     */
    @ManyToOne
    @JoinColumn(name = "admin_supplier_id")
    public AdminSupplier adminSupplier;

    /**
     * 所属店面
     */
    @JoinColumn(name="merchant_id")
    @ManyToOne
    public Merchant merchant;

    /**
     * 课程开始时间
     */
    @Column(name="start_time", length = 50)
    public String startTime;

    /**
     * 课程结束时间
     */
    @Column(name="end_time", length = 50)
    public String endTime;

    /**
     * 总课程数量
     */
    @Column(name="quantity")
    public Integer quantity;

    /**
     * 课程类别
     */
    @JoinColumn (name="curriculum_type_id")
    @ManyToOne
    public CurriculumType curriculumType;

    /**
     * 课程描述
     */
    @Column(name="remark", length = 255)
    public String remark;

    /**
     * 价格设置
     */
    @Column(name = "price")
    public Double price;

    /**
     * 课程负责人
     */
    @Column(name="charge_man",length = 255)
    public String chargeMan;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Curriculum> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.curriculumName like {curriculumName} ~/")
                .append("/~ and t.curriculumType = {curriculumType} ~/")
                .append("/~ and t.adminSupplier.id = {supplierId} ~/")
                .append("/~ and t.merchant.id = {merchantId} ~/");

        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Curriculum> orderPage = new JPAExtPaginator<Curriculum>("Curriculum t", "t", Curriculum.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }

    /**
     * Update
     * @param id
     * @param newObject
     */
    public static void update(Long id, Curriculum newObject) {
        Curriculum curriculum = Curriculum.findById(id);
        BeanCopy.beans(newObject, curriculum).ignoreNulls(true).copy();
        curriculum.save();
    }

    public static List<Curriculum> findAllCurriculumsByTypeAndMerchant(long typeId, long merchantId){
        return find("deleted = ? and merchant.id = ? and curriculumType.id = ? order by id", DeletedStatus.UN_DELETED, merchantId, typeId).fetch();
    }


}
